package com.keur.money.ui.user.act

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.adapter.MullaOnItemChildClickListener
import com.keur.money.base.utils.DensityUtils
import com.keur.money.databinding.ActMullaUserCouponListBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.MvpAct
import com.keur.money.ui.user.adapter.KeurUserCouponAdapter
import com.keur.money.ui.user.impl.KeurUserCouponPresenterImpl
import com.keur.money.ui.user.view.IKeurUserCouponView
import com.keur.money.utils.decoration.LinearSpacingItemDecoration
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/12 16:07
 * @Annotation：优惠券列表
 */
class KeurUserCouponAct: MvpAct<IKeurUserCouponView, KeurUserCouponPresenterImpl>(),IKeurUserCouponView {
    lateinit var binding: ActMullaUserCouponListBinding

    private var couponAdapter: KeurUserCouponAdapter? = null
    override fun createPresenter(): KeurUserCouponPresenterImpl {
        return KeurUserCouponPresenterImpl(this)
    }

    override fun getMullaRootView(): View {
        binding = ActMullaUserCouponListBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setMullaTextTitle(getString(R.string.coupon_list))
        setMullaBackBtnRes(R.mipmap.mulla_back_black)
        setMullaRightBtnVisibility(View.GONE)
        initMullaRefreshLayout()
        initMullaRecyclerView()

        val showKey = getString(R.string.coupon_list_empty_key)
        val showText = String.format(getString(R.string.coupon_list_empty),showKey)
        val startIndex = showText.indexOf(showKey)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FF8027FF")), startIndex, showText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(UnderlineSpan(), startIndex, showText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textEmpty.text = spString
    }

    override fun initMullaListener() {
        //没有数据提示
        binding.textEmpty.setOnClickListener {
            MyApp.app.publisher.publish(KeurEventData(KeurEventData.TYPE.KEUR_SHOW_COUPON_CENTER))
            finish()
        }
    }

    override fun initMullaData() {
        showMullaLoading(isTOuchOut = false, isCancel = true)
        presenter.loadMullaUserCouponListAction()

        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(licenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }


    /* 初始化刷新 */
    private fun initMullaRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            presenter.loadMullaUserCouponListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化优惠券列表
     */
    private fun initMullaRecyclerView() {
//        binding.recyclerView.setHasFixedSize(true)
//        binding.recyclerView.isNestedScrollingEnabled = false
//        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        val topDecoration = DensityUtils.dp2px(10f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, topDecoration, 0))
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        couponAdapter = KeurUserCouponAdapter(binding.recyclerView)
        binding.recyclerView.adapter = couponAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        couponAdapter?.setOnItemChildClickListener(MullaOnItemChildClickListener { _, childView, position ->
            val item = couponAdapter?.getItem(position)
            when (childView.id) {
                R.id.itemLayout -> {//点击Item

                }

                R.id.btnUse -> {//去使用
                    MyApp.app.publisher.publish(KeurEventData(KeurEventData.TYPE.KEUR_SHOW_REPAY))
                    finish()
                }
            }
        })
    }

    /**
     * 设置优惠券列表
     */
    override fun showMullaUserCouponList(couponList: ArrayList<JSONObject>) {
        rootViewBinding.containerLayout.setBackgroundColor(Color.parseColor("#FFF8F8F8"))
        binding.recyclerView.visibility = View.VISIBLE
        binding.emptyLayout.visibility = View.GONE
        couponAdapter?.data = couponList
    }

    /**
     * 设置空页面
     */
    override fun showMullaUserCouponEmpty() {
        rootViewBinding.containerLayout.setBackgroundColor(Color.parseColor("#FFFFFFFF"))
        binding.recyclerView.visibility = View.GONE
        binding.emptyLayout.visibility = View.VISIBLE
    }

    override fun hideMullaLoading() {
        super.hideMullaLoading()
        binding.refreshLayout.finishRefresh()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_COUPON_LIST_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_COUPON_LIST_END)
    }
}